﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Broadsides2d
{
    public class TradeNPC : NPC
    {
        private Port home;
        private Port dest;

        public static Texture2D texture;
        private int distanceTraveled;
        private int destination;

        public TradeNPC(int x, int y, Port dest, int gold, Port home) : base(x,y,ShipType.neutral)
        {

            this.distanceTraveled = 0;
            this.destination = dest.getPortId();
            this.dest = dest;
            this.home = home;
            this.setGold(gold);
        }

        public override void Update()
        {
            if (home.routes[destination] != null && home.getPortId() != destination)
            {
                
                float dx = (home.routes[destination].ElementAt(distanceTraveled).getX() - x) % (float)World.world.worldSizeInPixels;
                float dy = (y - home.routes[destination].ElementAt(distanceTraveled).getY());

                if (dx > World.world.worldSizeInPixels / 2 || dx < -World.world.worldSizeInPixels / 2)
                    dx *= -1;
                if (dy > World.world.worldSizeInPixels / 2 || dy < - World.world.worldSizeInPixels/2)
                    dy *= -1;

                setRotation((float)Math.Atan2(dx, dy));

                for (int i = -2; i < (((this.getRotation() > MathHelper.PiOver4 && this.getRotation() < 3 * MathHelper.PiOver4) || (this.getRotation() > 5 * MathHelper.PiOver4 && this.getRotation() < 7 * MathHelper.PiOver4)) ? 4 : 3); i++)
                    for (int j = -2; j < (((this.getRotation() > MathHelper.PiOver4 && this.getRotation() < 3 * MathHelper.PiOver4) || (this.getRotation() > 5 * MathHelper.PiOver4 && this.getRotation() < 7 * MathHelper.PiOver4)) ? 3 : 4); j++)
                        if (World.world.tiles[((int)((this.getX() / 8) + i) + World.world.worldSize) % World.world.worldSize, ((int)((this.getY() / 8) + j) + World.world.worldSize) % World.world.worldSize].getX() == home.routes[destination].ElementAt(distanceTraveled).getX() &&
                            World.world.tiles[((int)((this.getX() / 8) + i) + World.world.worldSize) % World.world.worldSize, ((int)((this.getY() / 8) + j) + World.world.worldSize) % World.world.worldSize].getY() == home.routes[destination].ElementAt(distanceTraveled).getY())
                        {
                            i = 10;
                            j = 10;
                            distanceTraveled++;
                        }
                if (distanceTraveled >= home.routes[destination].Count)
                {
                    home.traded();
                    clearTiles();
                    Game1.npcShips.Remove(this);
                }

            }

            base.Update();

            if (dest.getPixelX() - 2* Tile.SIZE < x && dest.getPixelX() + 2 * Tile.SIZE > x && dest.getPixelY() - 2 * Tile.SIZE < y && dest.getPixelY() + 2 * Tile.SIZE > y)
            {
                dest.traded();
                clearTiles();
                Game1.npcShips.Remove(this);
            }
        }

        public override void Draw(SpriteBatch batch)
        {
            //TEXTURE LOGIC HERE
            Vector2 v = Game1.recenter(x, y);

            v.X = ((v.X + World.world.worldSizeInPixels) % World.world.worldSizeInPixels);
            v.Y = ((v.Y + World.world.worldSizeInPixels) % World.world.worldSizeInPixels);
            batch.Draw(texture, v, null, Color.White, rotation, new Vector2(texture.Width / 2, texture.Height / 2), scale, SpriteEffects.None, 0.0f);
        }

        public override void explodeShip()
        {
            clearTiles();
            Game1.npcShips.Remove(this);
        }
    }
}
